<?php

namespace App\Admin\Controllers;

use App\Models\Category;
use Encore\Admin\Form;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Handlers\SlugTranslateHandler;
use Illuminate\Http\Request;
use App\Admin\Extensions\Tools\makeCategoryLetter;

class CategoryController extends AdminController
{
    protected function title()
    {
        return '栏目';
    }

    public function index(Content $content)
    {
        return $content
            ->title($this->title())
            ->description($this->description['index'] ?? trans('admin.list'))
            ->body(Category::tree());
    }

    protected function form()
    {
        $form = new Form(new Category);

        $form->select('pid', '父级栏目')->options(Category::selectOptions());
        $form->text('name', '栏目名称');
        $form->text('letter', '分类拼音')->prepend(new makeCategoryLetter());
        $form->image('thumb', '栏目图片');
        $form->text('keywords', '关键字');
        $form->text('description', '描述');

        return $form;
    }

    /**
     * 通过百度翻译和拼音转换成字母
     */
    public function makeCategoryLetter(SlugTranslateHandler $translate, Request $request)
    {
        $slug = $translate->translate($request->name);
        return success('翻译成功', compact('slug'));
    }
}
